package cn.xshi.sys.web;

import cn.xshi.common.entity.AreaRegionEntity;
import cn.xshi.common.entity.ConstantEntity;
import cn.xshi.common.entity.DataDictionaryEntity;
import cn.xshi.common.entity.PathEntity;
import cn.xshi.sys.model.SysConstant;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.util.FastJsonUtils;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.CommonUtils;
import cn.xshi.sys.service.SysConstantService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
 * @Desc 通用
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/sysCommon")
@Api(value = "通用API",tags = "通用API",description = "通用API")
public class SysCommonController extends BaseAction {

	@Resource
    CommonUtils commonUtils;

	@Resource
	SysConstantService sysConstantService;

	/**
	 * 查询字典集合（根据cnKey）
	 * @param cnKey
	 * @return
	 */
	@ApiOperation(value="获取字典集合（根据cnKey）", notes="获取字典集合（根据cnKey）")
	@GetMapping(value="/sysDataDictionary/list/{cnKey}")
	@Auth(value = "/sysCommon/SysDataDictionary/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult getSysDataDictionaryList(@PathVariable("cnKey")String cnKey){
		return outItemsStr(commonUtils.getSysDataDictionaryCache(cnKey));
	}

	/**
	 * 全部路径
	 * @return
	 */
	@ApiOperation(value="查询全部路径", notes="查询全部路径")
	@GetMapping(value="/path/list")
	@Auth(value = "/sysCommon/path/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult pathList(String key){
		List<PathEntity> pathList = commonUtils.getSysPathCache(key);
		return new BaseResult(FastJsonUtils.toJSONString(pathList));
	}

	/**
	 * 读取所有省份
	 * @return
	 */
	@ApiOperation(value="查询全部省份", notes="查询全部省份")
	@GetMapping(value="/pList")
	@Auth(value = "/sysCommon/pList",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult getPList(){
		List<AreaRegionEntity> list = commonUtils.getSysAreaRegionCache(null);
		return outItemsStr(list);
	}

	/**
	 * 读取城市
	 * @return
	 */
	@ApiOperation(value="查询全部城市", notes="查询全部城市")
	@GetMapping(value="/cList/{parentId}")
	@Auth(value = "/sysCommon/cList",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult getCList(@PathVariable("parentId") String parentId){
		List<AreaRegionEntity> list = new ArrayList<AreaRegionEntity>();
		if(StringUtil.isEmpty(parentId)){
			return outItemsStr(list);
		}
		list = commonUtils.getSysAreaRegionCache(parentId);
		return outItemsStr(list);
	}

	/**
	 * 读取区县
	 * @return
	 */
	@ApiOperation(value="查询全部区县", notes="查询全部区县")
	@GetMapping(value="/dList/{parentId}")
	@Auth(value = "/sysCommon/dList",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult getDList(@PathVariable("parentId") String parentId){
		List<AreaRegionEntity> list = new ArrayList<AreaRegionEntity>();
		if(StringUtil.isEmpty(parentId)){
			return outItemsStr(list);
		}
		list = commonUtils.getSysAreaRegionCache(parentId);
		return outItemsStr(list);
	}

	/**
	 * 根据Key查询常量
	 * @param key
	 * @return
	 */
	@ApiOperation(value="根据Key查询常量", notes="根据Key查询常量")
	@GetMapping(value="/constantByKey/{key}")
	@Auth(value = "/sysCommon/constantByKey",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public ConstantEntity getConstantCache(@PathVariable("key") String key){
		return commonUtils.getSysConstantCache(key);
	}

	/**
	 * 根据Id查询常量
	 * @param id
	 * @return
	 */
	@ApiOperation(value="根据Id查询常量", notes="根据Id查询常量")
	@GetMapping(value="/constant/{id}")
	@Auth(value = "/sysCommon/constant",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public SysConstant getConstant(@PathVariable("id") String id){
		if(StringUtil.isEmpty(id)){
			return new SysConstant();
		}
		SysConstant sysConstant = sysConstantService.getSysConstantById(id);
		return sysConstant;
	}

	/**
	 * 获取全部公共功能 通过缓存获取
	 * @param request
	 * @return
	 */
	@ApiOperation(value="查询全部功能", notes="查询全部功能")
	@GetMapping(value="/functioninfoCommon/list")
	@Auth(value = "/sysCommon/functioninfoCommon/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public String getConstant(HttpServletRequest request){
		return commonUtils.getSysFunctionInfoCommonCache();
	}

	/**
	 * 判断IP是否为黑户
	 * @param ip
	 * @return
	 */
	@ApiOperation(value="判断IP是否为黑户", notes="判断IP是否为黑户")
	@GetMapping(value="/ipFrozen/validate/{ip}")
	@Auth(value = "/sysCommon/ipFrozen/validate",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public boolean validateIp(@PathVariable("ip") String ip){
		return commonUtils.getSysIpFrozenCache(ip);
	}

	/**
	 * 根据KEY获取平台字典
	 * @param key
	 * @return
	 */
	@ApiOperation(value="根据KEY获取平台字典", notes="根据KEY获取平台字典")
	@GetMapping(value="/dataDictionary/list/{key}")
	@Auth(value = "/sysCommon/dataDictionary/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public List<DataDictionaryEntity> getSysDataDictionaryCache(@PathVariable("key") String key) {
		return commonUtils.getSysDataDictionaryCache(key);
	}

	/**
	 * 根据KEY获取平台路径
	 * @param key
	 * @return
	 */
	@ApiOperation(value="根据KEY获取平台路径", notes="根据KEY获取平台路径")
	@GetMapping(value="/path/list/{key}")
	@Auth(value = "/sysCommon/path/list",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public List<PathEntity> getSysPathCache(@PathVariable("key") String key) {
		return commonUtils.getSysPathCache(key);
	}
}
